let globalLet = {
    gui: document.getElementById("gui"),
    score: 0,
    backGround: null,
    btnRestart: new Button(btnRestartPos[0][0], btnRestartPos[0][1], "重新开始"),
    btnLeft: new Button(btnLeftPos[0]*btnSize[0]+btnWholePos[0], btnLeftPos[1]*btnSize[1]+btnWholePos[1], "<-"),
    btnRight: new Button(btnRightPos[0]*btnSize[0]+btnWholePos[0], btnRightPos[1]*btnSize[1]+btnWholePos[1], "->"),
    btnRotate: new Button(btnClockPos[0]*btnSize[0]+btnWholePos[0], btnClockPos[1]*btnSize[1]+btnWholePos[1], "顺时针旋转"),
    btnCcwRotate: new Button(btnCcwPos[0]*btnSize[0]+btnWholePos[0], btnCcwPos[1]*btnSize[1]+btnWholePos[1], "逆时针旋转"),
    btnQuicker: new Button(btnQuickerPos[0]*btnSize[0]+btnWholePos[0], btnQuickerPos[1]*btnSize[1]+btnWholePos[1], "加速"),
    nextText: document.getElementById("next-block-text"),
    overStyle: document.getElementById("gameover").style,
    buttons: null
};

globalLet.gui.onmousedown = function(event) {
    let x = event.clientX - guiX;
    let y = event.clientY - guiY;
    for (let btn of globalLet.buttons) {
        btn.onMouseDown([x, y]);
    }
    if (globalLet.btnQuicker.contains([x, y])) {
        globalLet.backGround.block.quicker();
    }
}

document.body.onmouseup = function(event) {
    let x = event.clientX - guiX;
    let y = event.clientY - guiY;
    for (let btn of globalLet.buttons) {
        btn.onMouseUp([x, y]);
    }
    globalLet.backGround.block.lower();
}

globalLet.gui.onmousemove = function(event) {
    let x = event.clientX - guiX;
    let y = event.clientY - guiY;
    this.style.cursor = "default";
    for (let btn of globalLet.buttons) {
        if (btn.contains([x, y])) {
            this.style.cursor = "pointer";
        }
    }
}

function updateSnapshot() {     // 绘制这一帧的图像
    let gui = globalLet.gui;
    if (gui.getContext) {
        let context = gui.getContext("2d");
        context.fillStyle = backGlobal;
        context.fillRect(0, 0, guiWidth, guiHeight);
        let backGround = globalLet.backGround;
        backGround.draw(context);
        for (let btn of globalLet.buttons) {
            btn.draw(context);
        }
        if (backGround.gameOver) {
            let over = document.getElementById("gameover");
            over.style.display = "block";
            over.innerHTML = "game over of " + globalLet.score + " points";
            globalLet.nextText.style.display = "none";
        }
        else {
            context.fillStyle = nextBack;
            context.fillRect(nextRect[0], nextRect[1], nextRect[2], nextRect[3]);
            backGround.nextBlockBuffer.draw(context);
        }
        document.getElementById("score").innerHTML = "分数：" + globalLet.score;
    }
}

function frameUpdate() {        // 更新每一帧
    updateSnapshot();
    let backGround = globalLet.backGround;
    backGround.update();
    if (globalLet.btnRestart.isClicked()) {
        restart();
    }
    else if (backGround.gameOver) {
        globalLet.buttons = [globalLet.btnRestart];
        globalLet.btnRestart.moveTo(btnRestartPos[1]);
    }
    else {
        globalLet.score += backGround.getAddScore();
        if (globalLet.btnLeft.isClicked()) {
            backGround.blockMove(-1);
        }
        else if (globalLet.btnRight.isClicked()) {
            backGround.blockMove(1);
        }
        else if (globalLet.btnRotate.isClicked()) {
            backGround.blockRotate(1);
        }
        else if (globalLet.btnCcwRotate.isClicked()) {
            backGround.blockRotate(-1);
        }
    }
    requestAnimationFrame(frameUpdate);
}

function restart() {        // 重开
    globalLet.score = 0;
    globalLet.backGround = new BackGround();
    globalLet.btnRestart.moveTo(btnRestartPos[0]);
    globalLet.buttons = [globalLet.btnRestart, globalLet.btnLeft, globalLet.btnRight, globalLet.btnRotate, globalLet.btnCcwRotate, globalLet.btnQuicker];
    document.getElementById("gameover").style.display = "none";
    globalLet.nextText.style.display = "block";
    for (let btn of globalLet.buttons) {
        btn.clear();
    }
}

globalLet.gui.width = guiWidth;
globalLet.gui.height = guiHeight;
globalLet.gui.style.left = guiX + "px";
globalLet.gui.style.top = guiY + "px";
globalLet.nextText.style.top = nextRect[1] + (nextRect[3]-nextLHeight)/2 + "px";
globalLet.nextText.style.lineHeight = nextLHeight + "px";
restart();
requestAnimationFrame(frameUpdate);
